package com.github.szhittech.service.repo;

import com.github.szhittech.MainPlugin;
import com.github.szhittech.bean.ClifeMavenResolver;
import com.github.szhittech.service.task.TaskApi;
import com.github.szhittech.util.Logc;

import org.apache.tools.ant.types.resources.Archives;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectList;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.ConfigurationPublications;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.artifacts.ResolutionStrategy;
import org.gradle.api.artifacts.dsl.ArtifactHandler;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.artifacts.maven.MavenPom;
import org.gradle.api.artifacts.maven.MavenResolver;
import org.gradle.api.artifacts.repositories.ArtifactRepository;
import org.gradle.api.artifacts.repositories.FlatDirectoryArtifactRepository;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.artifacts.repositories.MavenRepositoryContentDescriptor;
import org.gradle.api.artifacts.repositories.RepositoryContentDescriptor;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.component.Artifact;
import org.gradle.api.internal.artifacts.Module;
import org.gradle.api.internal.artifacts.configurations.ConfigurationInternal;
import org.gradle.api.internal.artifacts.ivyservice.projectmodule.DefaultProjectPublication;
import org.gradle.api.internal.component.MavenPublishingAwareContext;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.publish.maven.MavenPomDeveloper;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.internal.MavenPublishServices;
import org.gradle.api.publish.maven.internal.publication.MavenPublicationInternal;
import org.gradle.api.publish.maven.internal.publisher.MavenPublisher;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
import org.gradle.api.publish.maven.tasks.GenerateMavenPom;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.Upload;
import org.gradle.internal.Describables;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.SortedSet;

public class RepoApi {
    public static void init(MainPlugin mainPlugin) {
        ProjectInternal project = mainPlugin.getProject();
        project.getRepositories().maven(new Action<MavenArtifactRepository>() {
            @Override
            public void execute(MavenArtifactRepository mavenArtifactRepository) {
                mavenArtifactRepository.setUrl("../repo");
            }
        });

        project.getConfigurationActions().add(new Action<ProjectInternal>() {
            @Override
            public void execute(ProjectInternal projectInternal) {
                configureUploadArchivesTask(mainPlugin);
            }
        });

    }

    private static void configureUploadArchivesTask(MainPlugin mainPlugin) {
        TaskCollection<Upload> uploadTask = mainPlugin.getProject().getTasks().withType(Upload.class);
        if (uploadTask == null) {
            Logc.e(">>>> uploadTaskis null>");
            return;
        }
        Upload uploadArchives = uploadTask.findByName("uploadArchives");
//        Archives archives = new Archives();

        Logc.e(">>>> uploadArchives is " + uploadArchives);
        if (uploadArchives == null) {
            return;
        }
        Logc.e("======>>>" + mainPlugin.getProject().getName());


        Configuration config = uploadArchives.getConfiguration();
        if (config == null) {
            Logc.e(">>>> Configuration is null>");
            return;
        }
        Logc.e("======>>>Configuration is " + config);
        ConfigurationInternal configuration = (ConfigurationInternal) config;
        if (configuration == null) {
            Logc.e(">>>> configuration is null>");
            return;
        }
        Logc.e("======>>>ConfigurationInternal is " + configuration);
        Module module = configuration.getModule();
        if (module == null) {
            Logc.e(">>>> module is null>");
            return;
        }
        Logc.e("======>>>module is " + module);
        FlatDirectoryArtifactRepository nsm = uploadArchives.getRepositories().flatDir(new Action<FlatDirectoryArtifactRepository>() {
            @Override
            public void execute(FlatDirectoryArtifactRepository flatDirectoryArtifactRepository) {
                 flatDirectoryArtifactRepository.dir("../zepo");
            }
        });
        Logc.e("======>>>names is "+nsm.getDirs().toString());

        NamedDomainObjectList<MavenResolver> maven = uploadArchives.getRepositories().withType(MavenResolver.class);

        Iterator var5 = maven.iterator();
        Logc.e("======>>>MavenResolver is " +var5);
         /*
        while (var5.hasNext()) {
            MavenResolver resolver = (MavenResolver) var5.next();
            Logc.e("======>>>MavenResolver is "+resolver.getName());
            MavenPom pom = resolver.getPom();
            Logc.e("============configureUploadArchivesTask===getArtifactId>>>>>>" + pom.getArtifactId());
            Logc.e("============configureUploadArchivesTask===getGroupId>>>>>>" + pom.getGroupId());
            Logc.e("============configureUploadArchivesTask===getVersion>>>>>>" + pom.getVersion());

            Logc.e("============configureUploadArchivesTask===getName>>>>>>" + module.getName());
            Logc.e("============configureUploadArchivesTask===getGroup>>>>>>" + module.getGroup());
            Logc.e("============configureUploadArchivesTask===getVersion>>>>>>" + module.getVersion());
            pom.setGroupId("com.github.szhittech");
            pom.setArtifactId("hetxxxsdk");
            pom.setVersion("0.0.4-SNAPSHOT");

            String arg0 = pom.getGroupId().equals("unknown") ? module.getGroup() : pom.getGroupId();
            String arg1 = pom.getArtifactId().equals("empty-project") ? module.getName() : pom.getArtifactId();
            String arg2 = pom.getVersion().equals("0") ? module.getVersion() : pom.getVersion();
            ModuleVersionIdentifier publicationId = mainPlugin.getModuleIdentifierFactory().moduleWithVersion(arg0, arg1, arg2);
            mainPlugin.getPublicationRegistry().registerPublication(mainPlugin.getProject(), new DefaultProjectPublication(Describables.withTypeAndName("Maven repository", resolver.getName()), publicationId, true));
        }
         */
         GenerateMavenPom pol;

        upload(mainPlugin);
    }

    private static void upload(MainPlugin mainPlugin){
        String groupId = "com.github.szhittech";
        String ArtifactId = "hetxxxsdk";
        String version = "0.0.5-SNAPSHOT";
        String mavenDeployer = "mavenDeployer";
        Logc.e("==+++++========configureUploadArchivesTask===getArtifactId>>>>>>" +ArtifactId);
        Logc.e("===+++++=========configureUploadArchivesTask===getGroupId>>>>>>" + groupId);
        Logc.e("===+++++=========configureUploadArchivesTask===getVersion>>>>>>" + version);
        ModuleVersionIdentifier publicationId = mainPlugin.getModuleIdentifierFactory().moduleWithVersion(groupId, ArtifactId, version);
        mainPlugin.getPublicationRegistry().registerPublication(mainPlugin.getProject(),
                new DefaultProjectPublication(Describables.withTypeAndName("Maven repository", mavenDeployer), publicationId, true));
    }
}
